package com.wq.search.sevice;

import com.wq.search.dao.ArticleDao;
import com.wq.search.entity.Article;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ArticleService {
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    //添加索引
    public void insert(Article article){
        articleDao.save(article);
    }
    //分页
    public Page search(int page,int size,String keywords){
        PageRequest of = PageRequest.of(page, size);
        return articleDao.findByTitleOrContentLike(keywords,keywords,of);
    }
    //查询id
    public Article findById(String id) {
        return articleDao.findById(id).get();
    }
    //修改
    public void update(Article article) {
        articleDao.save(article);
    }
    //删除
    public void delete(String id) {
        articleDao.deleteById(id);
    }

    //高亮
    public List<Article>getHight(String keywords) {
        //        构建了一个查询对象
        BoolQueryBuilder should = QueryBuilders.boolQuery()
                .should(QueryBuilders.matchQuery("title", keywords))
                .should(QueryBuilders.matchQuery("content", keywords));
//        构建的查询高亮对象
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        NativeSearchQuery build = nativeSearchQueryBuilder.withQuery(should)
                .withHighlightFields(new HighlightBuilder
                                .Field("title")
                                .preTags("<span style='color:red'>")
                                .postTags("</span>"),
                        new HighlightBuilder
                                .Field("content")
                                .preTags("<span style='color:red'>")
                                .postTags("</span>"))
                .build();
//通过elasticsearchRestTemplate构建查询
        SearchHits<Article> searchHits = elasticsearchRestTemplate.search(build, Article.class);
//        构建一个空集合，用于添加要返回的article
        ArrayList<Article> articles = new ArrayList<>();

        for (SearchHit<Article> searchHit : searchHits) {
            Map<String, List<String>> highlightFields = searchHit.getHighlightFields();
            searchHit.getContent().setTitle(highlightFields.get("title") == null ? searchHit.getContent().getTitle() : highlightFields.get("title").get(0));
            searchHit.getContent().setContent(highlightFields.get("content") == null ? searchHit.getContent().getContent() : highlightFields.get("content").get(0));
            articles.add(searchHit.getContent());
        }
        return articles;
    }
}
